# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=maturin
pkgbase=mingw-w64-python-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-python-${_realname}
pkgver=1.2.3
pkgrel=3
pkgdesc='Build and publish crates with pyo3, rust-cpython and cffi bindings as well as rust binaries as python packages (mingw-w64)'
url='https://github.com/pyo3/maturin'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:MIT OR Apache-2.0')
depends=(
    "${MINGW_PACKAGE_PREFIX}-python")
makedepends=(
    "${MINGW_PACKAGE_PREFIX}-python-build"
    "${MINGW_PACKAGE_PREFIX}-python-installer"
    "${MINGW_PACKAGE_PREFIX}-python-wheel"
    "${MINGW_PACKAGE_PREFIX}-python-setuptools-rust")
options=(!strip)
source=("https://pypi.io/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "001-fix-default-target-on-mingw-32bit.patch")
sha256sums=('ef3f42af453d64f233b99543c3001bee645019a9c2022c7972210a9cacb5301f'
            '5d363f2540f84651439f47782c9d60ababde58f7187b09945040f3795745bf45')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/001-fix-default-target-on-mingw-32bit.patch"
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  # the features pull in "ring" which is not available on arm64
  # https://github.com/msys2/MINGW-packages/pull/18245#issuecomment-1690765429
  if [[ ${MSYSTEM} != CLANGARM64 ]]; then
    # this is the default feature set in Cargo.toml
    export MATURIN_SETUP_ARGS="--features full,rustls"
  fi

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 license-apache "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/license-apache"
  install -Dm644 license-mit "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/license-mit"
}
